//
//  CustomMenuView.m
//  DaZhePrivateHomeCuisineClient
//
//  Created by zouzhisheng on 14-12-14.
//  Copyright (c) 2014年 zouzhisheng. All rights reserved.
//

#import "CustomMenuView.h"

@implementation CustomMenuView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame WithTitleArray:(NSArray *)titles
{
    self=[self initWithFrame:frame];
    _titles=titles;
    if (self) {
        [self addSubViews];
        _menuArray=[NSMutableArray array];
        self.backgroundColor=[UIColor colorWithHexString:@"F7F7F7"];
    }
    return self;
}
-(void)addSubViews
{
    float width=SCREEN_WIDTH/_titles.count;
    float height=40.0;
    if (!_btArray) {
        _btArray=[NSMutableArray array];
    }
    for (int i=0; i<_titles.count; i++) {
        UIButton *bt=[UIButton buttonWithType:UIButtonTypeCustom];
        [bt setTitle:_titles[i] forState:UIControlStateNormal];
        [bt setTitleColor:[UIColor colorWithHexString:@"222222"] forState:UIControlStateNormal];
        bt.frame=CGRectMake(width*i, 0, width, height);
        bt.tag=100+i;
        bt.titleLabel.font=[UIFont systemFontOfSize:13];
        UIImageView *arrowImageView=[[UIImageView alloc] initWithFrame:CGRectMake(width-12, 18, 7, 3)];
        if (i==1) {
            arrowImageView.left=width-12;
        }
        arrowImageView.image=[UIImage imageNamed:@"down_arrow"];
        [bt addSubview:arrowImageView];
//        [bt setImage:[UIImage imageNamed:@"down_arrow"] forState:UIControlStateNormal];
//        [bt setImageEdgeInsets:UIEdgeInsetsMake(0, 50, 0, 10)];
        [bt addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        [_btArray addObject:bt];
        [self addSubview:bt];
    }
    
}
-(void)tapAction:(UIButton*)bt
{
    _currentBt=bt;
    if ([_delegate respondsToSelector:@selector(tapType:)]) {
        [_delegate tapType:bt.tag];
    }
       if (self.lineView.hidden) {
           self.lineView.hidden=NO;
           self.lineView.left=(bt.tag-100)*80;
    }
    [UIView animateWithDuration:0.2 animations:^{
      self.menuListView.height=200;
        arrowImage.alpha=1.0;
      self.lineView.left=(bt.tag-100)*80;
    }];
}
#pragma mark-getter
-(UIView*)lineView
{
    if (!_lineView) {
        _lineView=[[UIView alloc] initWithFrame:CGRectMake(0, 39, 80, 1)];
        _lineView.backgroundColor=[UIColor grayColor];
        [self addSubview:_lineView];
        
    }
    return _lineView;
}
-(UITableView*)menuListView
{
    if (!_menuListView) {
        _menuListView=[[UITableView alloc] initWithFrame:CGRectMake(0, self.bottom, SCREEN_WIDTH, 1)];
        _menuListView.delegate=self;
        _menuListView.dataSource=self;
        _menuListView.bounces=NO;
        _menuListView.separatorStyle=UITableViewCellSeparatorStyleNone;
        arrowImage=[[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2,self.bottom+200-10, 16, 5)];
        arrowImage.image=[UIImage imageNamed:@"down_arrow"];
        [self.superview addSubview:_menuListView];
        [self.superview addSubview:arrowImage];
        arrowImage.alpha=0;
        
    }
    return _menuListView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
#pragma mark - table datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _menuArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"DropDownMenuCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.contentView.backgroundColor=[UIColor colorWithHexString:@"EEEEEE"];
    id obj=_menuArray[indexPath.row];
    if ([obj isKindOfClass:[DZAreaSubModel class]]) {
        DZAreaSubModel *model=obj;
        cell.textLabel.text=model.name;
    }
    if ([obj isKindOfClass:[DZOtherCategoryInfoSubModel class]]) {
        DZOtherCategoryInfoSubModel *model=obj;
        cell.textLabel.text=model.name;
    }
    if ([obj isEqual:@"全部"]) {
         cell.textLabel.text=@"全部";
    }
    cell.textLabel.font=[UIFont systemFontOfSize:13];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     id obj=_menuArray[indexPath.row];
    if ([obj isKindOfClass:[DZAreaSubModel class]]) {
        DZAreaSubModel *model=obj;
        if ([_delegate respondsToSelector:@selector(selectDir:)]) {
            NSDictionary *dir=[NSDictionary dictionaryWithObjectsAndKeys:model.aid,@"area", nil];
            [_delegate selectDir:dir];
        }
        [_currentBt setTitle:model.name forState:UIControlStateNormal];
    }
    if ([obj isKindOfClass:[DZOtherCategoryInfoSubModel class]]) {
        DZOtherCategoryInfoSubModel *model=obj;
        if ([_delegate respondsToSelector:@selector(selectDir:)]) {
            NSString *key=nil;
            switch ([model.catid intValue]) {
                case  3:
                    key=@"dangci";
                    break;
                case  6:
                    key=@"tesecai";
                    break;
                case  7:
                    key=@"caipin";
                    break;
                    
                default:
                    break;
            }
            
            NSDictionary *dir=[NSDictionary dictionaryWithObjectsAndKeys:model.attid,key, nil];
            [_delegate selectDir:dir];
        }
         [_currentBt setTitle:model.name forState:UIControlStateNormal];
    }
    if ([obj isEqual:@"全部"]) {
         NSUInteger index=[_btArray indexOfObject:_currentBt];
        NSString *key=nil;
        switch (index) {
            case 0:
               key=@"caipin";
                break;
            case 1:
               key=@"tesecai";
                break;
            case 2:
               key=@"dangci";
                break;
            case 3:
                key=@"area";
                break;
            default:
                break;
        }
        if ([_delegate respondsToSelector:@selector(selectDir:)]) {
            NSDictionary *dir=[NSDictionary dictionaryWithObjectsAndKeys:@"",key, nil];
            [_delegate selectDir:dir];
        }
        [_currentBt setTitle:_titles[index] forState:UIControlStateNormal];
        
    }
    [UIView animateWithDuration:0.2 animations:^{
        self.menuListView.height=0;
        arrowImage.alpha=0.0;
        self.lineView.hidden=YES;
    }];
}
@end
